package org.adw.launcher2.ld.secret.ui;

import java.util.LinkedList;
import java.util.List;

import org.adw.launcher2.ld.secret.db.DatabaseHelper;
import org.adw.launcher2.ld.secret.db.MyContactCache;
import org.adw.launcher2.ld.secret.dom.MyContact;
import org.adw.launcher2.ld.secret.dom.contact.UserInfo;
import org.adw.launcher2.ld.secret.ui.adapter.MyContactAdapter;
import org.adw.launcher2.ld.secret.ui.utils.Global;

import org.adw.launcher2.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;

/*
 * Use for test
 */
public class MyHiddenContactActivity extends Activity {
	public final String TAG = getClass().getSimpleName();
	public MyContactAdapter contactAdapter;
	public List<UserInfo> listAfterRestore;
	private EditText filterText = null;
	boolean isFilter = false;
	public ListView listView;
	public ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_info);
		listView = (ListView) findViewById(R.id.listMyContact);

		Global.cr = getContentResolver();
		// Init db for test something related to database
		Global.dbHelper = DatabaseHelper.getInstance(this);
		filterText = (EditText) findViewById(R.id.txtSearchContact);
		filterText.addTextChangedListener(watcher);
		loadUserInfoThread();
	}

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {

		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable arg0) {
			List<UserInfo> listFilter = new LinkedList<UserInfo>();
			for (int i = 0; i < listAfterRestore.size(); i++) {
				String keyWord = arg0.toString().toLowerCase();
				if (listAfterRestore.get(i).getUserName().toLowerCase()
						.contains(keyWord)) {
					listFilter.add(listAfterRestore.get(i));
				}
			}
			bindView(listFilter);

		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
		filterText.removeTextChangedListener(watcher);
		Global.dbHelper.close();
	}

	protected List<UserInfo> loadUserInfoThread() {
		final List<UserInfo> list = new LinkedList<UserInfo>();
		AsyncTask<Uri, Void, List<UserInfo>> task = new AsyncTask<Uri, Void, List<UserInfo>>() {
			@Override
			protected void onPreExecute() {
				dialog = ProgressDialog.show(MyHiddenContactActivity.this, "",
						"Please wait for few seconds...", true);
			}

			@Override
			protected List<UserInfo> doInBackground(Uri... uris) {

				return MyContactCache.getInstance(Global.dbHelper).getUserInfo();
			}

			@Override
			protected void onPostExecute(List<UserInfo> result) {
				listAfterRestore = result;
				bindView(result);
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		};
		task.execute();
		return list;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.my_hidden_contact_menu, menu);// lua chon menu
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.restore_contact:
			restoreAllSelectedContacts();
			break;
		case R.id.refresh_hidden_contact:
			loadUserInfoThread();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void restoreAllSelectedContacts() {
		AsyncTask<Uri, Void, Void> task = new AsyncTask<Uri, Void, Void>() {
			@Override
			protected void onPreExecute() {
				dialog = ProgressDialog.show(MyHiddenContactActivity.this, "",
						"Please wait for few seconds...", true);
			}

			@Override
			protected Void doInBackground(Uri... params) {
				for (int i = 0; i < MyContactAdapter.listToHide.size(); i++) {
					long id = MyContactAdapter.listToHide.get(i).getUserId();
					MyContact myContact = new MyContact();
					myContact.id = id;
					myContact.restore();
					listAfterRestore.remove(MyContactAdapter.listToHide.get(i));
				}
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				bindView(listAfterRestore);
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		};
		task.execute();
	}

	protected void bindView(List<UserInfo> list) {
		contactAdapter = new MyContactAdapter(this, R.layout.contact_info,
				list);
		listView.setAdapter(contactAdapter);
	}
}
